package demo2;

import sun.rmi.runtime.Log;

public class LogIn {
    private String userName = "admin";
    private String password = "123456";

    /*public void loginInfo(String userName, String password) {
        if (!this.userName.equals(userName)) {
            System.out.println("用户名有误");
        }
        if (!this.password.equals(password)) {
            System.out.println("密码有误");
        }

        System.out.println("登陆成功");
    }*/
    //loginInfo()可以用异常来表示，但是java中并没有用户名异常、密码异常
    //所以此时我们需要自定义异常

    public void loginInfo(String userName, String password) throws UserNameException,PasswordException{
        if (!this.userName.equals(userName)) {
            throw new UserNameException("用户名错误");
        }
        if (!this.password.equals(password)) {
           throw new PasswordException("密码有误");
        }

        System.out.println("登陆成功");
    }
    public static void main(String[] args) {
        LogIn logIn=new LogIn();
        //logIn.loginInfo("admin", "123456");
        try{
            logIn.loginInfo("admin", "123456");
        }catch (UserNameException e){
            e.printStackTrace();
            System.out.println("UserNmaeException");
        }catch (PasswordException e){
            e.printStackTrace();
            System.out.println("PasswordException");
        }
    }
}